package com.enjoy.dome.base;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.enjoy.dome.StartActivity;
import com.enjoy.dome.manage.AppManager;
import com.enjoy.dome.app.MyApp;
import com.enjoy.dome.bean.base.MessageEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public abstract class BaseActivity extends AppCompatActivity {

    @Override
    protected final void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.getInstance().addActivity(this);//添加ACT进管理站
        switch (MyApp.getInstance().getAppStatus()) {
            case MyApp.STATUS_FORCE_KILLED:
                restartApp();
                break;
            case MyApp.STATUS_NORMAL:
                onCreateNormal(savedInstanceState);
                break;
        }
    }

    /**
     * 描述：正常模式启动App
     *
     * @param savedInstanceState
     */
    protected void onCreateNormal(@Nullable Bundle savedInstanceState) {
        int layoutId = getLayoutId();
        if (layoutId > 0) setContentView(layoutId);
        //注册通知
        if (isRegisterEventBus()) {
            EventBus.getDefault().register(this);
        }
        initCreate(savedInstanceState);
        initView();
        initListener();
        initData();
    }


    @Override
    protected final void onDestroy() {
        super.onDestroy();
        AppManager.getInstance().delActivity(this);//管理站移除
        if (MyApp.getInstance().getAppStatus() == MyApp.STATUS_NORMAL) {
            if (isRegisterEventBus()) {//销毁事件
                EventBus.getDefault().unregister(this);
            }
        }
    }

    protected abstract int getLayoutId();

    protected abstract void initCreate(Bundle savedInstanceState);

    protected abstract void initView();

    protected abstract void initListener();

    protected abstract void initData();



    /**
     * 重启应用
     */
    protected void restartApp() {
        Intent intent = new Intent(this, StartActivity.class);
        startActivity(intent);
        finish();
    }

    /**
     * 接收分发事件
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusCome(MessageEvent event) {

    }

    /**
     * 字体防变形
     */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {//字体大小处理
        if (newConfig.fontScale != 1)//非默认值
            getResources();
        super.onConfigurationChanged(newConfig);
    }

    /**
     * 字体防变形
     */
    @Override
    public Resources getResources() {//字体大小处理
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }

    /**
     * 是否注册事件分发
     */
    protected boolean isRegisterEventBus() {
        return false;
    }
}
